package com.htwz.webapp

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import java.util.HashSet

/**
 * Created by laijian on 2017/8/14.
 * SharedPreferences 数据保存
 */
object SPUtil {

    private const val TAB_NAME = "webapp"

    fun putBase(context: Context,keyName: String, value: Any): Boolean {
        val sharedPreferences = getSp(context)
        val editor: SharedPreferences.Editor = sharedPreferences.edit()
        when (value) {
            is Int -> editor.putInt(keyName, value)
            is Boolean -> editor.putBoolean(keyName, value)
            is Float -> editor.putFloat(keyName, value)
            is String -> editor.putString(keyName, value)
            is Long -> editor.putLong(keyName, value)
            else -> throw IllegalArgumentException("SharedPreferences can,t be save this type")
        }
        return editor.commit()
    }

    fun getString(context: Context, keyName: String, defaultValue: String? = null): String {
        val sharedPreferences = getSp(context)
        return sharedPreferences.getString(keyName, defaultValue) ?: ""
    }


    fun getSp(context: Context): SharedPreferences {
        return context.getSharedPreferences(TAB_NAME, Activity.MODE_PRIVATE)
    }
}